package tn.orange.canreminder.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class PlaceDaoImp implements PlaceDao {

	private Context context;

	String[] columns = new String[] { GenericDAO.KEY_ID, Place.COL_ADRESSE,
			Place.COL_CATEGORY, Place.COL_IMAGE, Place.COL_LATITUDE,
			Place.COL_LONGITUDE, Place.COL_NOM, Place.COL_TELEPHONE, };

	public PlaceDaoImp(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		GenericDAO.instance = null;
		GenericDAO.getInstance(context, Place.DATABASE_NAME,
				Place.TABLE_CREATE, Place.DATABASE_TABLE,
				Place.DATABASE_VERSION);

	}

	@Override
	public List<Place> getAllPlace() {
		// TODO Auto-generated method stub

		try {
			GenericDAO dao = GenericDAO.getInstance(context,
					Place.DATABASE_NAME, Place.TABLE_CREATE,
					Place.DATABASE_TABLE, Place.DATABASE_VERSION);

			List<Place> places = new ArrayList<Place>();
			Cursor cursor = dao.get(Place.DATABASE_TABLE, columns);
			if (cursor != null) {
				if (cursor.moveToFirst()) {

					int idColumn = cursor.getColumnIndex(GenericDAO.KEY_ID);
					int idAdresse = cursor.getColumnIndex(Place.COL_ADRESSE);
					int idCategory = cursor.getColumnIndex(Place.COL_CATEGORY);

					int idnom = cursor.getColumnIndex(Place.COL_NOM);
					int idlongitude = cursor
							.getColumnIndex(Place.COL_LONGITUDE);
					int imageColumn = cursor.getColumnIndex(Place.COL_IMAGE);

					int latitudeColumn = cursor
							.getColumnIndex(Place.COL_LATITUDE);
					int telephoneColumn = cursor
							.getColumnIndex(Place.COL_TELEPHONE);

					int count = cursor.getCount();

					for (int i = 0; i < count; i++) {

						long id = cursor.getLong(idColumn);
						String adresse = cursor.getString(idAdresse);
						String category = cursor.getString(idCategory);
						String latitude = cursor.getString(latitudeColumn);
						String longitude = cursor.getString(idlongitude);
						String image = cursor.getString(imageColumn);
						String telephone = cursor.getString(telephoneColumn);
						String nom = cursor.getString(idnom);

						Place place = new Place();
						place.setId(id);
						place.setAdresse(adresse);
						place.setCategory(category);
						place.setImage(image);
						place.setLatitude(latitude);
						place.setLongitude(longitude);
						place.setNom(nom);
						place.setTelephone(telephone);
						places.add(place);
						cursor.moveToNext();

					}
				}
			}
			dao.close();
			return places;
		}

		catch (Exception e) {
			// TODO: handle exception
			System.out.println();
		}
		return null;

	}

	@Override
	public void insertPlace(Place place) {
		// TODO Auto-generated method stub

		// TODO Auto-generated method stub

		GenericDAO dao = GenericDAO.getInstance(context, Place.DATABASE_NAME,
				Place.TABLE_CREATE, Place.DATABASE_TABLE,
				Place.DATABASE_VERSION);
		try {

			if (dao != null) {

				ContentValues values = new ContentValues();

				values.put(Place.COL_ADRESSE, place.getAdresse());

				values.put(Place.COL_CATEGORY, place.getCategory());
				values.put(Place.COL_IMAGE, place.getImage());

				values.put(Place.COL_LATITUDE, place.getLatitude());
				values.put(Place.COL_LONGITUDE, place.getLongitude());

				values.put(Place.COL_NOM, place.getNom());

				values.put(Place.COL_TELEPHONE, place.getTelephone());

				dao.insert(Place.DATABASE_TABLE, values);

			}
		} catch (Exception e) {
			// TODO: handle exception

		}
		dao.close();

	}

	@Override
	public void deletePlace(long id) {
		// TODO Auto-generated method stub
		GenericDAO dao = GenericDAO.getInstance(context, Place.DATABASE_NAME,
				Place.TABLE_CREATE, Place.DATABASE_TABLE,
				Place.DATABASE_VERSION);
		dao.delete(Place.DATABASE_TABLE, id);
		dao.close();

	}

	@Override
	public void deletePlace(String string) {
		// TODO Auto-generated method stub
		GenericDAO dao = GenericDAO.getInstance(context, Place.DATABASE_NAME,
				Place.TABLE_CREATE, Place.DATABASE_TABLE,
				Place.DATABASE_VERSION);
		dao.getWritableDatabase().delete(Place.DATABASE_TABLE,
				"nom='" + string + "'", null);
		dao.close();
	}
}
